/**
 * 课程管理
 */
import {BASEURL, BASEIMGURL} from "@/api/baseUrl";
import {searchClasses, searchIndustry} from "@/api/regManagementApi";
import {
    pcCourseList,
    getCtListByCourceId,
    update,
    addCourse,
    updateCourse,
    deleteCourse
} from "@/api/classManagementApi";
import CommonUtils from "@/utils/CommonUtils";

export default {
    data() {
        return {
            classOptions: [],
            industryOptions: [],
            classNumOption: [],
            classNumSection: null,//班级人数区间
            baseImgUrl: BASEIMGURL,
            baseApiUrl: BASEURL + '/user/updateUserPhoto',
            addFromVisible: false,
            editCourseTimeDialog: {//编辑课时弹窗
                visible: false,
                courseId: null,
                courseTimeLists: []
            },
            courseTimeForm: {//修改课时对象
                id: null,
                introduce: null,
                url: null,
                title: null
            },

            options: [{
                value: '选项1',
                label: '黄金糕'
            }, {
                value: '选项2',
                label: '双皮奶'
            }],
            searchForm: {
                proId: null,
                label: null
            },
            tableData: [],
            multipleSelection: [],
            //新增课程
            addFrom: {
                isReUploadImg: false,//是否为重新上传图片（修改时）
                id: null,
                courseName: null,
                classes: null,
                proId: null,
                proName: null,
                classNum: null,
                label: null,
                capacity: null,
                courseTime: null,
                img: null
            },
            //分页
            page: 1,
            size: 10,
            total: 0,
            //验证
            rules: {
                courseName: [
                    {required: true, message: '请输入课程名称', trigger: 'blur'},
                ],
                label: [
                    {required: true, message: '请输入关键词', trigger: 'blur'},
                ],
                capacity: [
                    {required: true, message: '请输入班级人数', trigger: 'blur'},
                ],
                classes: [
                    {required: true, message: '请选择班级', trigger: 'change'}
                ],
                proId: [
                    {required: true, message: '请选择行业名称', trigger: 'change'}
                ],
                classNum: [
                    {required: true, message: '请选择课时', trigger: 'change'}
                ],
                zuanye: [
                    {required: true, message: '请选择专业', trigger: 'change'}
                ],
                courseTime: [
                    {required: true, message: '请选择日期', trigger: 'change'}
                ],
                img: [
                    {required: true, message: '请选择课程封面', trigger: 'blur'}
                ],
            },
        };
    },
    created() {
        this.getClassList();
        this.getIndustryList();
        this.searchCourseList();
    },
    mounted() {
        this.initClassNumOption();
    },
    methods: {
        //初始化班级人数选项
        initClassNumOption() {
            this.classNumOption = [{
                label: '全部',
                value: {minNum: null, maxNum: null}

            }, {
                label: '0-50人',
                value: {minNum: 0, maxNum: 50}

            }, {
                label: '50-200人',
                value: {minNum: 50, maxNum: 200}
            }, {
                label: '200-500人',
                value: {minNum: 200, maxNum: 500}
            }, {
                label: '500人以上',
                value: {minNum: 500, maxNum: 100000}
            }]
        },

        //查询班级
        getClassList() {
            searchClasses().then(res => {
                this.classOptions = [...this.classOptions, ...res];
            }, err => {
            })
        },
        //查询行业
        getIndustryList() {
            searchIndustry().then(res => {
                this.industryOptions = [...this.industryOptions, ...res];
            }, err => {
            })
        },
        //查询课程数据
        searchCourseList() {
            let param = {
                page: this.page,
                size: this.size,
                maxNum: this.classNumSection ? this.classNumSection.value.maxNum : null,
                minNum: this.classNumSection ? this.classNumSection.value.minNum : null,
                proId: this.searchForm.proId,
                label: this.searchForm.label
            }
            pcCourseList(param).then(res => {
                this.tableData = res.list;
                this.total = res.total;
            }, err => {
            })
        },
        //新增课程
        addOneCourse() {
            this.addFromVisible = true;
            this.addFrom.id = null;
            //清除上传的upload空间
            if (this.$refs['uploadCourseImg']) {
                this.$refs['uploadCourseImg'].clearFiles();
            }
        },
        //班级人数 || 专业（行业） change
        conditionChange() {
            this.searchCourseList();
        },
        //关键字查询
        searchByLabelChange() {
            this.classNumSection = null;
            this.searchForm.proId = null;
            this.searchCourseList();
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        handleEdit(row) {
            this.editCourseTimeDialog.visible = true;
            if (this.$refs['courseTimeForm']) {
                this.$refs['courseTimeForm'].resetFields();
            }
            this.editCourseTimeDialog.courseId = row.id;
            this.editCourseTimeDialog.courseTimeLists = [];
            this.searchCourseTimeLists();
        },
        //修改
        modifyCourse(row) {
            this.addFrom = row;
            this.addFrom.isReUploadImg = true;
            this.addFrom.label = this.addFrom.label.replace(/,/g, '#');
            if (this.$refs['uploadCourseImg']) {
                this.$refs['uploadCourseImg'].clearFiles();
            }
            this.addFromVisible = true;
        },
        //删除课程
        delCourse(row) {
            this.$confirm('课程删除后将无法恢复，是否确定删除？', '重要提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let param = {id: row.id}
                deleteCourse(param).then(res => {
                    this.$message.success("删除成功");
                    this.page = CommonUtils.getPageNum(this.total, this.size, this.page);
                    this.searchCourseList();
                }, err => {
                })
            })
        },

        //查询课程课时
        searchCourseTimeLists() {
            let param = {
                courseId: this.editCourseTimeDialog.courseId
            }
            getCtListByCourceId(param).then(res => {
                this.editCourseTimeDialog.courseTimeLists = res;
            }, err => {
            })
        },
        //获取课时是否已编辑
        getCourseTImeStatus(item) {
            let res = item.cname;
            if (!item.introduce) {
                res += '     ----未编辑';
            }
            return res;
        },
        handleDelete(index, row) {
            console.log(index, row);
        },
        //分页
        handleSizeChange(val) {
            this.size = val;
            this.searchCourseList();
        },
        handleCurrentChange(val) {
            this.page = val;
            this.searchCourseList();
        },
        //新增
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.addFrom.label = this.addFrom.label.replace(/#/g, ',');
                    this.addFrom.proName = this.industryOptions.find(f => f.id === this.addFrom.proId).name;
                    this.addFrom.id ? this.updateCourse() : this.addCourse();
                } else {
                    this.$message.warning("请将信息填写完整");
                }
            });
        },
        //新增课程
        addCourse() {
            addCourse(this.addFrom).then(res => {
                this.$message.success("课程创建成功");
                this.$refs['addFrom'].resetFields();
                this.addFromVisible = false;
                this.page = 1;
                this.searchCourseList();
            }, err => {
            })
        },
        //修改课程
        updateCourse() {
            updateCourse(this.addFrom).then(res => {
                this.$message.success("课程修改成功");
                this.$refs['addFrom'].resetFields();
                this.addFromVisible = false;
                this.searchCourseList();
            }, err => {
            })
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        //编辑课时
        editSubmitForm() {
            this.courseTimeForm.title = this.editCourseTimeDialog.courseTimeLists.find(f => f.id === this.courseTimeForm.id).cname;
            update(this.courseTimeForm).then(res => {
                this.$message.success("编辑成功");
                this.editCourseTimeDialog.visible = false;
            }, err => {
            })

        },
        //上传前
        beforeUpload() {
            this.addFrom.isReUploadImg = false;
            //为修改
            if (this.addFrom.id) {
                this.addFrom.img = null;
            }
        },
        // 文件上传成功
        createCourseSuccess(res, file) {
            this.addFrom.img = res.result;
        },
        //超出限制
        handlePictureCardPreview() {
            if (this.addFrom.img) {
                this.$message.warning("暂且仅支持一张封面");
                return false;
            }
        },
        handleRemove(file, fileList) {
            console.log(file)
            console.log(fileList)
            this.addFrom.img = null;
        },
        //获取状态对应的label
        getStatusLabel(status) {
            let label = '';
            switch (status) {
                case 0:
                    label = '待审核';
                    break;
                case 1:
                    label = '审核通过';
                    break;
                case 2:
                    label = '不通过';
                    break;
            }
            return label;
        },
        //获取状态对应的样式
        getStatusCss(status) {
            let c = '';
            switch (status) {
                case 0:
                    c = 'c-e09017'
                    break;
                case 1:
                    c = 'c-0f65fa'
                    break;
                case 2:
                    c = 'c-ff0000'
                    break;
            }
            return c;
        },
        //上传课时视频前
        courseTimeBeforeUpload(file) {
            let accept = '.mp4,.3gp,.avi,.rmvb,.mkv,.wmv';
            let postfix = file.name.split('.')[1];
            if (accept.indexOf(postfix) == -1) {
                this.$message.error(`请上传：${accept}格式视频文件`);
                return false;
            }
        },
        //上传课时视频成功后
        createcourseTimeSuccess(res, file) {
            this.courseTimeForm.url = res.result;
        },
        //超出上传课时视频个数回调
        courseTimeOnExceed() {
            this.$message.warning("目前暂且仅支持单个视频");
        }
    }
}